axios = require('axios')
PG = require('fckoa/db/pg')

module.exports = require('fckoa/router') {
get:{
    "/" : ( ctx ) =>
        ctx.body = {
            "ctx.req.url":ctx.req.url
            "ctx.path":ctx.path
        }
    # http请求 , 文档 https://github.com/axios/axios
    fetch : (ctx)=>
        {data} = await axios.get('https://www.baidu.com/robots.txt')
        ctx.body = data

    # 读数据库 , 为了方便数据库的使用，在 src/db/pg.coffee 中自定义了一些助手函数，请查阅源码
    db : (ctx)=>
        ctx.body = await PG.rows(
            """
SELECT name, n FROM (
SELECT COUNT(1) AS n,kind_id FROM fordeal_good GROUP BY kind_id
) GOOD JOIN fordeal_kind ON kind_id=fordeal_kind.id ORDER BY n DESC;
            """
        )

    # 返回错误
    error : (ctx)=>
        raise mail:1

    # http://127.0.0.1:9999/demo/query?a=1
    query : (ctx)=>
        console.log ctx.query
        console.log ctx.querystring
        console.log ctx.path
        console.log ctx.url

        ctx.body = ctx.query
}
post:{
test : (ctx)=>
    ctx.body = ctx.req.body
}
}
